public class test02 {
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
        public List<Integer> postorderTraversal(TreeNode root) {
            //迭代法
            Stack<TreeNode> stack = new Stack<>();
            List<Integer> res = new ArrayList<>();
            if(root==null){
                return res;
            }
            stack.push(root);
            while(!stack.isEmpty()){
                TreeNode tmpNode = stack.pop();
                res.add(tmpNode.val);
                if(tmpNode.left!=null){
                    stack.push(tmpNode.left);
                }
                if(tmpNode.right!=null){
                    stack.push(tmpNode.right);
                }
            }
            //颠倒(list集合)
            Collections.reverse(res);
            return res;
        }
}
